<style>
    .dropdown-menu{
        z-index: 9999;
    }
</style>
<div class="row-fluid clearfix">
    <div class="col-md-12 column">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页</a>
            </li>
            <li class="active">
                公告管理
            </li>
        </ul>
    </div>
    <div class="col-md-12 column">
        <div class="col-sm-12 div-page">
            <div class="col-sm-12 div-table">
                <div id="custom-toolbar">
                    <!--根据角色权限动态初始化-->
                </div>
                <table id="table" class="table table-hover table-condensed" data-toolbar="#custom-toolbar" data-click-to-select="true"  data-single-select="true" data-height="450">
                    <thead>
                        <tr>
                            <th data-field="state" data-checkbox="true"></th>
                            <th data-field="AreaText" data-halign="center" data-align="center" data-width="120">地区</th>
                            <th data-field="StartDate" data-halign="center" data-align="center" data-width="120">开始日期</th>
                            <th data-field="EndDate" data-halign="center" data-align="center" data-width="120">结束日期</th>
                            <th data-field="Title" data-halign="center" data-align="center" data-sortable="true" data-width="200">标题</th>
                            <th data-field="Content" data-halign="center" data-align="center" data-sortable="true" data-width="400">内容</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
        <div class="col-sm-12 alert alert-success div-alert" id="events-result" data-es="Aquí se muestra el resultado del evento"></div>
    </div>
</div>

<script>
    $(function () {

        /*获取当前角色的按钮权限_add_ywq_20150331-Start*/
        var btnHtml ;
        var btnRole = JSON.parse($.cookie("Role_note"));
        btnHtml = '<div class="form-inline" role="form">';
        btnHtml += (btnRole["CommonDataAdd"]) ? '<button id="btn-CommonDataAdd"  type="button" role="button" class="subFun btn btn-sm btn-primary">' +
                '<span class="glyphicon glyphicon-plus"></span> 新增</button> ' : '' ;
        btnHtml += (btnRole["CommonDataModify"]) ? '<button id="btn-CommonDataModify" type="button" role="button" class="subFun btn btn-sm btn-Data btn-success disabled">' +
                '<span class="glyphicon glyphicon-pencil"></span> 修改</button> ' : '' ;
        btnHtml += (btnRole["CommonDataCancel"]) ? '<button id="btn-CommonDataCancel"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-danger disabled" >' +
                '<span class="glyphicon glyphicon-trash"></span> 删除</button> ' : '' ;
        btnHtml += '<button id="btn-CommonDataDetail"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-info disabled" >' +
                '<span class="glyphicon glyphicon-trash"></span> 详情</button> ' ;
        btnHtml+='</div>';
        $("#custom-toolbar").append(btnHtml);
        /*获取当前角色的按钮权限_add_ywq_20150331-End*/

        /*表格初始化_add_ywq_20150331-Start*/
        $('#table').bootstrapTable({
            //url: globalUrl+'api/Advertisement',
            url:"../json/NoticeList.json",
            responseHandler:function(resp){
                return resp.data;
            },
            pagination:true,
            search:true
        })
        .on('click-row.bs.table', function (e, row, $element) {
            if ( $element.hasClass('selected') ) {
                $element.removeClass('info');
            }
            else {
                $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                $element.addClass('info');
            }
        })
        .on('check.bs.table', function (e, row, $element) {
            $(".btn-Data").removeClass("disabled");
        })
        .on('uncheck.bs.table', function (e, row, $element) {
            $(".btn-Data").addClass("disabled");
        });
        /*表格初始化_add_ywq_20150331-End*/

        /*功能按钮的事件注册_add_ywq_20150331-Start*/
        $('.subFun ').on("click",function () {

            var fileName=$(this).attr("id").substr(14);
            var url="../assets/tpls/Notice_"+fileName+".html";
            getTpls(url,function(resp){
                $("#modal-Template").html(resp);
                if($.cookie("OperClass")!=1||$.cookie("OperClass")!=1)
                {
                    $("#Area").parent().parent().hide();
                }
                FormatSelect(globalUrl+"api/Site?orgid=910",$("#Area"),"全部","",function(resp){});
                $('#StartDate').datepicker({
                    format:"yyyy.mm.dd"
                }).on('changeDate', function(ev){
                    setFieldsValid($("#validForm"),["StartDate"]);
                });
                $('#EndDate').datepicker({
                    format:"yyyy.mm.dd"
                }).on('changeDate', function(ev){
                    setFieldsValid($("#validForm"),["EndDate"]);
                });
                switch (fileName){
                    case "Add":
                        $('#validForm')
                                .formValidation({
                                    err: {
                                        container: 'popover'
                                    },
                                    icon: {
                                        valid: 'glyphicon glyphicon-ok',
                                        invalid: 'glyphicon glyphicon-remove',
                                        validating: 'glyphicon glyphicon-refresh'
                                    }
                                })
                                .on('success.form.fv', function(e) {
                                    e.preventDefault();
                                    $("#StartDate").val($("#StartDate").val().replace(/\./g,""));
                                    $("#EndDate").val($("#EndDate").val().replace(/\./g,""));
                                    if($("#StartDate")>$("#EndDate"))
                                    {
                                        alert("结束日期必须大于起始日期");
                                    }
                                    var $form = $(e.target);
                                    $.ajax({
                                        data:$form.serializeArray(),
                                        type: "post",
                                        url: globalUrl+"api/Advertisement",
                                        dataType: "json",
                                        success: function (resp) {
                                            if(resp.result)
                                            {
                                                $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Advertisement"});
                                                $('#modal-Template').modal('hide');
                                            }
                                            else
                                            {
                                                alert(resp.msg);
                                            }
                                        },
                                        error:function(){
                                            alert("更新数据失败！");
                                        }
                                    });
                                });
                        break;
                    case "Modify":
                        dataBindToModal($('#validForm'),$('#table'));
                        $('#validForm')
                                .formValidation()
                                .on('success.form.fv', function(e) {
                                    e.preventDefault({
                                        err: {
                                            container: 'popover'
                                        },
                                        icon: {
                                            valid: 'glyphicon glyphicon-ok',
                                            invalid: 'glyphicon glyphicon-remove',
                                            validating: 'glyphicon glyphicon-refresh'
                                        }
                                    });
                                    $("#StartDate").val($("#StartDate").val().replace(/\./g,""));
                                    $("#EndDate").val($("#EndDate").val().replace(/\./g,""));
                                    if($("#StartDate")>$("#EndDate"))
                                    {
                                        alert("结束日期必须大于起始日期");
                                    }
                                    var $form = $(e.target);
                                    var bv = $form.data('formValidation');
                                    $.ajax({
                                        data:$form.serializeArray(),
                                        type: "put",
                                        url: globalUrl+"api/Advertisement",
                                        dataType: "json",
                                        success: function (resp) {
                                            if(resp.result)
                                            {
                                                $('#table').bootstrapTable('refresh',{url: globalUrl+'api/Advertisement'});
                                                $('#modal-Template').modal('hide');
                                            }
                                            else
                                            {
                                                alert(resp.msg);
                                            }
                                        },
                                        error:function(){
                                            alert("更新数据失败！");
                                        }
                                    });


                                });
                        break;
                    case "Cancel":
                        dataBindToModal($('#validForm'),$('#table'));
                        $('#modal-btn-del').on("click",function () {
                            $.ajax({
                                type: "delete",
                                url: globalUrl+"api/Advertisement?AdvertId="+$('#table').bootstrapTable('getSelections')[0].AdvertId,
                                dataType: "json",
                                success: function (resp) {
                                    if(resp.result)
                                    {
                                        $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Advertisement"});
                                        $('#modal-Template').modal('hide');
                                    }
                                    else
                                    {
                                        alert(resp.msg);
                                    }
                                },
                                error:function(){
                                    alert("更新数据失败！");
                                }
                            });
                        });
                        break;
                    default:
                        dataBindToModal($('#validForm'),$('#table'));
                }
                $('#modal-Template').modal('show');
            });
        });
        /*功能按钮的事件注册_add_ywq_20150331-End*/
    });
</script>